class Yarn < ActiveRecord::Base
  belongs_to  :supplier
  belongs_to  :container_type
  has_many    :containers
  
  validates_uniqueness_of :code
  validates_presence_of   :name, :container_type, :supplier, :code 
  
  def after_initialize
    @inventory_state = ContainerState.find_by_code(0)
  end
  
  def yards_in_inventory
    self.containers.sum(:length_yield, :conditions => "container_state_id = #{@inventory_state.id}")
  end
  
  def yards_total
    self.containers.sum(:length_yield)
  end
  
  def gross_lbs_in_inventory
    self.containers.sum(:gross_weight, :conditions => "container_state_id = #{@inventory_state.id}")
  end
  
  def net_lbs_in_inventory
    self.containers.sum(:net_weight, :conditions => "container_state_id = #{@inventory_state.id}")
  end
  
  def containers_in_inventory
    self.containers.find(:all, :conditions => ["container_state_id = ?", @inventory_state.id])
  end
    
  
end
